package com.ukao.steward.ui.menuLeft.myEarnings;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.bean.BankCardListBena;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.pesenter.valetRunners.WithdrawPesenter;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.WithdrawView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


import static com.ukao.steward.R.id.bank_card_txt;
import static com.ukao.steward.R.id.pice;

/**
 * 余额提现
 */
public class WithdrawFragment extends MvpFragment<WithdrawPesenter> implements WithdrawView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.next_btn)
    Button nextBtn;
    Unbinder unbinder;
    @BindView(R.id.withdraw_count)
    EditText withdrawCount;

    @BindView(R.id.all_withdraw)
    TextView allWithdraw;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.bank_card_txt)
    TextView bankCardTxt;
    @BindView(R.id.minExtract_money_tv)
    TextView minExtractMoneyTv;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String withdrawBalance;

    boolean loadFinish = false;
    private String walletId;

    private List<BankCardListBena.ListBean> mListBean;
    private int minExtractMoney;
    private double minExtractMoneyDen;

    public static WithdrawFragment newInstance(String withdrawBalance, int  minExtractMoney) {
        WithdrawFragment fragment = new WithdrawFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, withdrawBalance);
        args.putInt(ARG_PARAM2, minExtractMoney);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            withdrawBalance = getArguments().getString(ARG_PARAM1);
            minExtractMoney = getArguments().getInt(ARG_PARAM2);
        }
    }

    @Override
    protected WithdrawPesenter createPresenter() {
        return new WithdrawPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_withdraw, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        tradeWalletlist();
    }

    @Override
    protected void initView() {
        if (minExtractMoney!=0) {
            minExtractMoneyDen=Double.parseDouble(CheckUtils.isEmptyNumber(minExtractMoney));
            minExtractMoneyTv.setText("提现金额不得低于"+CheckUtils.isEmptyNumber(minExtractMoney)+"元");
        }
        title.setText("余额提现");
        withdrawCount.setHint("可提现" + withdrawBalance + "元");
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
    }

    @Override
    protected void initListener() {
        super.initListener();
        withdrawCount.addTextChangedListener(mTextWatcher);
    }

    private TextWatcher mTextWatcher = new MyTextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            String   str=s.toString().trim();
            Constant.calculate(str, withdrawCount);
           double strPice=0;
            if (!str.isEmpty()){
                strPice=Double.parseDouble(str);
            }
            if (s.toString().trim().isEmpty()||strPice<minExtractMoneyDen) {
                if (nextBtn.isEnabled()) {
                    nextBtn.setAlpha(0.5f);
                    nextBtn.setEnabled(false);
                }

            } else if (!nextBtn.isEnabled()) {
                nextBtn.setAlpha(1f);
                nextBtn.setEnabled(true);
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn, R.id.next_btn, R.id.all_withdraw, bank_card_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.next_btn:
                if (CheckUtils.isEmpty(mListBean)) {
                    startForResult(FillBlankFragment.newInstance("", ""), Constant.REQUEST_CODE);
                } else {
                    try {
                        String strwithdrawCount = getText(withdrawCount);
                        double douwithdrawCount = Double.parseDouble(strwithdrawCount);
                        double douwithdrawBalance = Double.parseDouble(withdrawBalance);
                        if (douwithdrawBalance > 0 && douwithdrawBalance >= douwithdrawCount) {
                            mvpPresenter.tradeAuditapply(strwithdrawCount, walletId);
                        } else {
                            T.show("提现金额不足");
                        }

                    } catch (Exception e) {
                        T.show("金额格式错误");
                    }


                }


                break;
            case R.id.all_withdraw:
                withdrawCount.setText(withdrawBalance);
                break;
            case bank_card_txt:
                startForResult(BankCardListFragment.newInstance("", ""), Constant.BANEK_REQUEST_CODE);

                break;


        }
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE:
                    loadFinish = false;
                    tradeWalletlist();
//                    String bankCard = data.getString(Constant.ARG_PARAM1);
//                    bankCardTxt.setText(bankCard);
//                    bankCardTxt.setVisibility(View.VISIBLE);
                    break;
                case Constant.BANEK_REQUEST_CODE:
                    if (data==null)return;
                    String bankCard = data.getString(Constant.ARG_PARAM1,"");
                    if (!CheckUtils.isEmpty( data.getString(Constant.ARG_PARAM2,""))){
                        walletId =data.getString(Constant.ARG_PARAM2,"");
                    }
                    if (!CheckUtils.isEmpty(bankCard)){
                        bankCardTxt.setText(bankCard);
                    }




                    break;

            }
        }
    }

    public void tradeWalletlist() {
        if (loadFinish) return;
        mvpPresenter.bankCardList();

    }

    /**
     * 提现成功
     *
     * @param withdrawamount 提现金額
     * @param msg
     */
    @Override
    public void withdrawSucceed(String withdrawamount, String msg) {
        T.show(msg);
        Bundle mBundle = new Bundle();
        mBundle.putString(Constant.ARG_PARAM1, withdrawamount);
        setFragmentResult(RESULT_OK, mBundle);
        finish();
    }

    @Override
    public void bankCardListSucceed(List<BankCardListBena.ListBean> mListBean) {
        this.mListBean = mListBean;
        loadFinish = true;
        if (CheckUtils.isEmpty(mListBean)) {
            nextBtn.setText("下一步");
            bankCardTxt.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        } else {
            nextBtn.setText("确认提现");
            view.setVisibility(View.VISIBLE);
            bankCardTxt.setVisibility(View.VISIBLE);
            BankCardListBena.ListBean mitem = mListBean.get(0);
            bankCardTxt.setText(mitem.getAccountName()+"("+mitem.getAccountNo()+")");
            walletId = mitem.getId();
        }
    }
}
